package org.carabiner.infinitest.swingui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTree;
import javax.swing.tree.TreePath;

import org.carabiner.infinitest.InfinitestUtils;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a>
 */
class ClipboardCopyListener extends MouseAdapter implements ClipboardOwner
{
  public void mouseClicked(MouseEvent e)
  {
    if (e.getClickCount() == 2)
    {
      JTree tree = (JTree) e.getSource();
      TreePath path = tree.getPathForLocation(e.getX(), e.getY());
      InfinitestTreeNode node = (InfinitestTreeNode) path.getLastPathComponent();
      if (node.isLeaf())
      {
        Throwable error = node.getTestEvent().getError();
        StringSelection stringSelection = new StringSelection(InfinitestUtils.throwableToString(error));
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, this);
      }
    }
  }

  public void lostOwnership(Clipboard clipboard, Transferable contents)
  {
  }

}
